package com.icoopoo.utils.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.collections4.MapUtils;

/**
 * 
 * @作成者:icoopoo 
 * @Data:2018年12月4日 下午8:41:53
*
 */
public class PropertiesHelper {
	
	private String [] propertiesFilePathes;
	
	private Map<String, String> datas = new HashMap<String, String>();
	
	
	public PropertiesHelper(String... properties){
		this.propertiesFilePathes = properties;
		if(this.propertiesFilePathes != null){
			for(String p : propertiesFilePathes){
				this.init(p);
			}
		}
	}
	
	private void init( String p ) {
		try {
			if(p == null) return ;
			File file = new File(p);
			if(! file.exists()) return ;
			Properties config = new Properties();
			config.load(new FileInputStream(new File(p)));
			Enumeration<Object> enumeration = config.keys();
			while(enumeration.hasMoreElements()){
				String key = enumeration.nextElement().toString();
				datas.put(key, config.getProperty(key));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Map<String, String> getMapData(){
		return this.datas;
	}
	
	public String getString( String name ){
		return MapUtils.getString(this.datas, name);
	}
	
	public String getString(String name,String defaultVal){
		return MapUtils.getString(this.datas, name,defaultVal);
	}
	
	public Integer getInteger(String name){
		return MapUtils.getInteger(this.datas, name);
	}
	
	public Integer getInteger(String name,Integer defualtVal){
		return MapUtils.getInteger(this.datas, name,defualtVal);
	}
	
	public Long getLongeger(String name,Long defualtVal){
		return MapUtils.getLong(this.datas, name,defualtVal);
	}
	
	public Long getLongeger(String name){
		return MapUtils.getLong(this.datas, name);
	}
	
	public Double getDouble(String name){
		return MapUtils.getDouble(this.datas, name);
		
	}
	
	public Double getDouble(String name,Double defualVal){
		return MapUtils.getDouble(this.datas, name,defualVal);
	}
	
	public boolean getBoolean(String name,boolean defualVal){
		return MapUtils.getBooleanValue(this.datas, name,defualVal);
	}
	
	public boolean contains( String key ){
		return this.keys().contains(key);
	}
	
	public Set<String> keys(){
		return this.datas.keySet();
	}

}
